// pages/leader/leader_user.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    leader: {
      avatar_url: '',
      name: '',
      address: '',
      order_sum: 0,
      waiting_take: 0,
      today_money: 0,
      balance: 0,
    },
    page: 1,
    order_type: "waiting_take",
    orderList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getLeaderInfo(this.data.page, this.data.order_type)
  },

  /**
   * 上拉刷新
   */
  onPullDownRefresh: function() {
    let that = this
    wx.showNavigationBarLoading()
    that.getLeaderInfo(this.data.page, this.data.order_type)
    wx.hideNavigationBarLoading()
    wx.stopPullDownRefresh()
  },

  /**
   * 
   */
  onReachBottom: function() {
    let that = this,
      page = that.data.page
    page = page + 1
    that.setData({
      page: page
    })
    that.getLeaderInfo(that.data.page, that.data.order_type)
  },

  /**
   * 
   */
  getLeaderInfo: function(page, order_type) {
    let that = this,
      orderSum = 0,
      waitOrder = [],
      orderList = [],
      list = that.data.orderList,
      data, k, j
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/rgl-cmdr/center?access_token=' + accessToken,
        data: {
          page: page,
          order_type: order_type
        },
        success: res => {
          data = res.data.data
          if (res.data.status === 1) {
            that.setData({
              'leader.name': data.name,
              'leader.avatar_url': data.avatar,
              'leader.balance': data.balance,
              'leader.order_sum': data.orders.length,
              'leader.today_money': data.today_income,
              'leader.address': data.address
            })
            if (page == 1) {
              that.setData({
                orderList: data.orders
              })
            } else {
              for (k in data.orders) {
                list.push(data.orders[k])
              }
              that.setData({
                orderList: list
              })
            }
            wx.hideLoading()
          } else {
            wx.showToast({
              icon: 'none',
              title: res.data.msg,
            })
            wx.hideLoading()
          }
        },
        fail: res => {
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
          })
          wx.hideLoading()
        }
      })
    })
  },

  /**
   * 上拉刷新
   */
  onPullDownRefresh: function() {
    let that = this
    wx.showNavigationBarLoading()
    that.getAllData(that.data.type, that.data.category_id, that.data.page)
    wx.hideNavigationBarLoading()
    wx.stopPullDownRefresh()
  },

  /**
   * tabs
   */
  tab: function(e) {
    let that = this,
      orderType = e.currentTarget.dataset.type,
      page = 1
    that.setData({
      order_type: orderType
    })
    that.getLeaderInfo(page, orderType)
  },

  takeProduct: function(e) {
    let that = this,
      id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      success: res => {
        if (res.confirm) {
          app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
            wx.request({
              url: app.globalData.hostAPI + '/api/v1/order/confirm-take?access_token=' + accessToken,
              data: {
                order_id: id
              },
              success: res => {
                if (res.data.status === 1) {
                  that.getLeaderInfo(that.data.page, that.data.order_type)
                  wx.hideLoading()
                } else {
                  wx.hideLoading()
                  wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                  })
                }
              },
              fail: res => {
                wx.hideLoading()
                wx.showToast({
                  icon: 'none',
                  title: res.data.msg,
                })
              }
            })
          })
        }
      }
    })
  }
})